import { _decorator, Node, Vec3 } from 'cc';
import { PlayUser } from '../core/PlayUser';
import { EightBallGame } from './EightBallGame';
import { HitBallAttribute } from '../core/Game';
import { EventManager } from '../core/EventManager';
import { TableAtrribute } from '../core/TableAttribute';
import { BallAttribute, BallColor } from '../core/BallAttribute';
export class EndlessBallGame extends EightBallGame {
  constructor(node: Node) {
    super(node);
  }
  public initBallsPosition() {
    const snookerNode = this.billiamNode.getChildByName('SnookerBalls');
    snookerNode.active = false;

    const ballsNode = this.billiamNode.getChildByName('Balls');
    // 8球/9球
    ballsNode.active = true;
    const balls = ballsNode.children;
    for (const ball of balls) {
      ball.active = false;
    }
    this.checkOutHoleBalls(false, false, []);
    // 设置白球的位置
    EventManager.Instance().Emit('InitMainBallPosition', new Vec3(10, 0, 0));
  }
  public isTargetHitBall(color: BallColor, name: string) {
    return true;
  }
  // 击打球的位置
  public defaultAimBallPosition() {
    const pos = new Vec3(0, 0, 0);
    const balls = this.getActiveBalls();
    for (const ball of balls) {
      if (ball.position.x < 0) {
        ball.getWorldPosition(pos);
      }
    }
    return pos;
  }
  // 游戏是否已经结束
  public checkGameEnd() {
    return false;
  }
  // 是否进球得分
  public hasHoledScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    const balls = hitBallList;
    for (var i=balls.length-1; i>=0; i--) {
      if (balls[i].step === step) {
        if (balls[i].ba.isMainWhiteBall()) continue;
        return true;
      } else {
        break;
      }
    }
    return false;
  }
  public isIllegalHitBall(firstCollideBall: BallAttribute, hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    // 白球进洞就违规
    for (var i=hitBallList.length-1; i>=0; i--) {
      if (hitBallList[i].step !== step) { break; }
      if (hitBallList[i].ba.isMainWhiteBall()) {
        return { isIlleagel: true, message: '白球进洞，违规击球' };
      }
    }
    // 没有违规
    return { isIlleagel: false, message: 'ok' };
  }
  // 当前击球 和受保护 的球列表
  public getHitProtectBalls(color: BallColor) {
    const protectBalls = [];
    const balls = this.getActiveBalls();
    return { hitBalls: balls, protectBalls };
  }

  public getCurrentStepScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    var score = 0;
    const balls = hitBallList;
    for (var i=balls.length-1; i>=0; i--) {
      if (balls[i].step === step) {
        if (balls[i].ba.isMainWhiteBall()) {
          return 0;
        }
        score += 1;
      } else {
        break;
      }
    }
    return score;
  }

  public getUserHitColorText(color: BallColor) {
    return color.getColorText();
  }

  public swapCurrentUserColor(hasExchangeUser: boolean) {

  }
  public checkOutHoleBalls(isIgnoreColorStage: boolean, isIllegalHitBall: boolean, currentHitBalls: Array<HitBallAttribute>) {
    const aballs = this.getActiveBalls();
    if (aballs.length > 0) {
      return;
    }
    const mainBall = this.billiamNode.getChildByName('MainBall').getChildByName('WhiteBall').getChildByName('BallSphereMain');
    const randnum = Math.floor(Math.random() * 6) + 3;
    const ballsNode = this.billiamNode.getChildByName('Balls');
    const balls = ballsNode.children;
    var i = 0;
    while (i < randnum) {
      const fx = Math.random() > 0.5 ? 1 : -1;
      const fz = Math.random() > 0.5 ? 1 : -1;
      const pos = new Vec3(
        0.9 * TableAtrribute.DeskHalfAxisXLength * Math.random() * fx,
        0,
        0.9 * TableAtrribute.DeskHalfAxisZLength * Math.random() * fz,
      );
      const dist = (pos.x - mainBall.worldPosition.x) * (pos.x - mainBall.worldPosition.x) + (pos.z - mainBall.worldPosition.z) * (pos.z - mainBall.worldPosition.z);
      if (dist > 9) {
        EventManager.Instance().Emit("ResetBallPosition", {
          ballName: balls[i].name, pos,
        });
        i += 1;
      }
    }
  }
  // 球桌上当前活跃的球
  public getActiveBalls() {
    const bList: Node[] = [];
    const balls = this.billiamNode.getChildByName('Balls').children;
    for (const b of balls) {
      if (b.active) {
        bList.push(b);
      }
    }
    return bList;
  }
  public isExcellentLevel(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor, userId: string) {
    return 0;
    // 一杆进两个球以上 鼓掌 1
    // 连续进3个球，优秀 2
    const steps = [0, 0, 0];
    for (var i = hitBallList.length -1; i>=0; i--) {
      const hba = hitBallList[i];
      if (hba.ba.isMainWhiteBall()) {
        if (hba.step === step) {
          return 0;
        }
        continue;
      }
      if (hba.step === step) {
        steps[0] += 1;
      } else if (hba.step === step-1) {
        steps[1] += 1;
      } else if (hba.step === step-2) {
        steps[2] += 1;
      } else {
        break;
      }
    }
    if (steps[0] >= 2) return 1;
    if (steps[0] >= 1 && steps[1] >= 1 && steps[2] >= 1) return 2;
    return 0;
  }
  public isCurrentUserWin(side: number, isIlleagel: boolean) {
    return true;
  }
  public penalyScore() {
    return 0;
  }
  public getAllBallList() {
    return [];
  }
  public robotRenShu(playUsers: PlayUser[]) { return false; }
}

